﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace('Aricie.FriendlierUrlProvider.DraggableGroups');

Aricie.FriendlierUrlProvider.DraggableGroups = function (element) {
    Aricie.FriendlierUrlProvider.DraggableGroups.initializeBase(this, [element]);
    this._uniqueId = null;
    this._dragSource = null;
    this._dragTarget = null;
    this._checkingField = null;
    this._deletionMessage = null;
    this._isDragging = false;
    this._isOverDropTarget = false;
}

Aricie.FriendlierUrlProvider.DraggableGroups.prototype = {

    initialize: function () {
        Aricie.FriendlierUrlProvider.DraggableGroups.callBaseMethod(this, 'initialize');

        var currentDraggingBehaviour = this;
        jQuery("#" + this._dragTarget).sortable({
            revert: 100,
            containment: "parent",
            tolerance: "pointer",
            placeholder: "placeHolderSortable",
            forcePlaceholderSize: true,
            forceHelperSize: true,
            cursorAt: { right: 5, bottom: 5},
            activate: function (event, ui) {
                jQuery(event.target).addClass("FocusDuringDrag");
            },
            deactivate: function (event, ui) {
                jQuery(event.target).removeClass("FocusDuringDrag");
            },
            start: function (event, ui) {
                jQuery(event.target).addClass("FocusDuringDrag");
            },
            stop: function (event, ui) {
                jQuery(event.target).removeClass("FocusDuringDrag");
                // postback vers le serveur avec la liste des éléments si cette dernière a été changée
                var currentElements = jQuery("#" + currentDraggingBehaviour.get_dragTarget() + " li").map(function () {
                    if (jQuery(this).hasClass("UsedGroup")) {
                        return jQuery(this).attr("position");
                    } else {
                        return "NEW:" + jQuery(this).attr("position");
                    }
                }).get()

                if ("" + currentElements !== "" + currentDraggingBehaviour._checkingField) { // pour profiter du join sur les tableaux
                    __doPostBack(currentDraggingBehaviour.get_uniqueId(), "LIST:" + currentElements);
                }
            }
        });

        jQuery("#" + this._dragSource + " .AvailableGroup").draggable({
            appendTo: "#" + this._dragTarget,
            connectToSortable: "#" + this._dragTarget,
            helper: "clone",
            revert: "invalid",
            revertDuration: 100,
            cursorAt: { right: 5, bottom: 5 },
            opacity: 1,
            start: function (event, ui) {
                currentDraggingBehaviour._isDragging = true;
                jQuery(event.target.parentNode).addClass("IgnoreDuringDrag");
            },
            stop: function (event, ui) {
                currentDraggingBehaviour._isDragging = false;
                jQuery(event.target.parentNode).removeClass("IgnoreDuringDrag");
            }
        });

        jQuery("#" + this._dragSource + " ul, #" + this._dragSource + " li").disableSelection();

        jQuery("#" + this._dragTarget + " li.UsedGroup .UsedGroupEditItem").click(function () {
            __doPostBack(currentDraggingBehaviour.get_uniqueId(), "EDIT:" + jQuery(this).parent().attr("position"));
        });

        jQuery("#" + this._dragTarget + " li.UsedGroup .UsedGroupDeleteItem").click(function () {
            if (confirm(currentDraggingBehaviour.get_deletionMessage())) {
                __doPostBack(currentDraggingBehaviour.get_uniqueId(), "DELETE:" + jQuery(this).parent().attr("position"));
            }
        });

        jQuery("#" + this._dragSource + " ul.AvailableGroups li.GroupInfo ul.GroupGroups").each(function () {
            jQuery(this).position({
                my: "left top",
                at: "right top",
                of: jQuery(this).parent(),
                collision: "none"
            });
            jQuery(this).hide();
        });

        jQuery("#" + this._dragSource + " ul.AvailableGroups li.GroupInfo").hover(function () {
            if (!currentDraggingBehaviour._isDragging) {
                jQuery("ul.GroupGroups", this).show();
            }
        }, function () {
            if (!currentDraggingBehaviour._isDragging) {
                jQuery("ul.GroupGroups", this).hide();
            }
        });


        // on se souvient de la structure actuelle
        currentDraggingBehaviour._checkingField = jQuery("#" + currentDraggingBehaviour.get_dragTarget() + " li").map(function () { return jQuery(this).attr("position"); }).get();
    },

    dispose: function () {
        Aricie.FriendlierUrlProvider.DraggableGroups.callBaseMethod(this, 'dispose');
    },

    get_dragTarget: function () {
        return this._dragTarget;
    },

    set_dragTarget: function (value) {
        if (this._dragTarget !== value) {
            this._dragTarget = value;
            this.raisePropertyChanged('dragTarget');
        }
    },

    get_dragSource: function () {
        return this._dragSource;
    },

    set_dragSource: function (value) {
        if (this._dragSource !== value) {
            this._dragSource = value;
            this.raisePropertyChanged('dragSource');
        }
    },

    get_uniqueId: function () {
        return this._uniqueId;
    },

    set_uniqueId: function (value) {
        if (this._uniqueId !== value) {
            this._uniqueId = value;
            this.raisePropertyChanged('uniqueId');
        }
    },

    get_deletionMessage: function () {
        return this._deletionMessage;
    },

    set_deletionMessage: function (value) {
        if (this._deletionMessage !== value) {
            this._deletionMessage = value;
            this.raisePropertyChanged('deletionMessage');
        }
    }
}

Aricie.FriendlierUrlProvider.DraggableGroups.registerClass('Aricie.FriendlierUrlProvider.DraggableGroups', Sys.UI.Control);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();